package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {
    @Autowired
    private IStrategyDetailService strategyDetailService;
    @Autowired
    private IStrategyService strategyService;

    /**
     * 查看所有攻略细节
     * @param qo 
     * @return
     */
    @RequestMapping("/list")
    public Object list(@ModelAttribute("qo") QueryObject qo, Model model){
        qo.setOrderBy("catalog.sequence,detail.sequence");
        PageInfo<StrategyDetail> pageInfo = strategyDetailService.query(qo);
        List<Strategy> strategies = strategyService.selectAll();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("strategies",strategies);
        return "strategyDetail/list";
    }

    /**
     * 保存或更新推荐文章
     * @param detail 推荐信息
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail detail, MultipartFile file){
        strategyDetailService.saveOrUpdate(detail,file);
        return new JSONResult();
    }

    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContentById(Long id){
        return strategyDetailService.getContentById(id);
    }
}
